package com.igiantpanda.btserver;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    private BleManager mBleManager;

    // Peripheral Device
    private Button mBtnBleAdvertise;

    // Central Device
    private Button mBtnBleScan;
    private Button mBtnConnect;
    private Button mBtnWriteCharacter;
    private Button mBtnReadCharacter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        permissionCheck();
        initViews();
        mBleManager = new BleManager(this);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 1:
                Log.d(TAG, "BLUETOOTH grantResults" + grantResults.toString());
                break;
            case 2:
                Log.d(TAG, "BLUETOOTH_ADMIN grantResults" + grantResults.toString());
                break;
            case 3:
                Log.d(TAG, "ACCESS_FINE_LOCATION grantResults" + grantResults.toString());
                break;
            default:
                break;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void permissionCheck() {

        // Use this check to determine whether BLE is supported on the device.  Then you can
        // selectively disable BLE-related features.
        if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
            Toast.makeText(this, "ble_not_supported", Toast.LENGTH_SHORT).show();
            finish();
        }

        if (checkSelfPermission(Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.BLUETOOTH}, 1);
        }

        if (checkSelfPermission(Manifest.permission.BLUETOOTH_ADMIN) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.BLUETOOTH_ADMIN}, 2);
        }

        if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 3);
        }

    }

    private void initViews() {
        mBtnBleAdvertise = (Button) findViewById(R.id.btn_ble_advertise);
        mBtnBleAdvertise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mBleManager.startAdvertise(10000);
            }
        });

        mBtnBleScan = (Button) findViewById(R.id.btn_ble_scan);
        mBtnBleScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBleManager.startScan(10000);
            }
        });

        mBtnConnect = (Button) findViewById(R.id.btn_connect);
        mBtnConnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBleManager.connectDevice();
            }
        });

        mBtnReadCharacter = (Button) findViewById(R.id.btn_read_char);
        mBtnReadCharacter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBleManager.readCharacteristics();
            }
        });

        mBtnWriteCharacter = (Button) findViewById(R.id.btn_write_char);
        mBtnWriteCharacter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBleManager.writeCharacteristics();
            }
        });
    }
}